#!/bin/bash

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 切换到仓库根目录（假设脚本在仓库的shared_scripts目录下）
REPO_ROOT="$(dirname "$SCRIPT_DIR")"
cd "$REPO_ROOT" || {
    echo "Error: Failed to change to repository root directory" >&2
    exit 1
}

# 初始化结果数组
result=()

# 遍历hosts目录下的所有AI_model.json文件
while IFS= read -r -d '' model_file; do
    # 使用get_single_model_config.sh处理每个模型文件
    config=$(./shared_scripts/get_single_model_config.sh "$(realpath "$model_file")")
    if [[ $? -eq 0 ]]; then
        result+=("$config")
    else
        echo "Warning: Failed to process $model_file" >&2
    fi
done < <(find hosts -name "*AI_model.json" -print0)

# 输出合并后的JSON数组
if [[ ${#result[@]} -gt 0 ]]; then
    printf '%s\n' "${result[@]}" | jq -s .
else
    echo "Error: No valid model configurations found" >&2
    exit 1
fi
